package com.xs.aifitnessagent.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xs.aifitnessagent.entities.User;
import com.xs.aifitnessagent.entities.req.UserLoginReq;
import com.xs.aifitnessagent.entities.req.UserRegisterReq;
import com.xs.aifitnessagent.entities.vo.UserVO;
import com.xs.aifitnessagent.enums.ReturnCodeEnum;
import com.xs.aifitnessagent.mapper.UserMapper;
import com.xs.aifitnessagent.resp.ResultData;
import com.xs.aifitnessagent.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lambor
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public ResultData register(UserRegisterReq userRegisterReq) {
        // 1. 非空校验
        if (Objects.isNull(userRegisterReq)) {
            return ResultData.fail(ReturnCodeEnum.PARAMETER_ISNULL.getCode(),
                    ReturnCodeEnum.PARAMETER_ISNULL.getMessage());
        }

        // 2. 校验两次密码是否一致
        if (!userRegisterReq.getPassword().equals(userRegisterReq.getCheckPassword())) {
            return ResultData.fail(ReturnCodeEnum.BUSINESS_ERROR.getCode(),
                    "两次输入的密码不一致");
        }

        // 3. 校验用户名是否存在
        User one = this.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, userRegisterReq.getUsername()));
        if (!ObjectUtil.isEmpty(one)) {
            return ResultData.fail(ReturnCodeEnum.USERNAME_EXIST.getCode(),
                    ReturnCodeEnum.USERNAME_EXIST.getMessage());
        }

        // 4. 执行数据库添加操作
        // 4.1 密码加密
        User user = new User();
        BeanUtil.copyProperties(userRegisterReq, user);
        user.setPassword(DigestUtil.md5Hex(userRegisterReq.getPassword()));
        // 4.2 保存用户
        boolean saved = this.save(user);
        if (saved) {
            return ResultData.success(true);
        } else {
            return ResultData.fail(ReturnCodeEnum.RC205.getCode(), ReturnCodeEnum.RC205.getMessage());
        }
    }

    @Override
    public List<UserVO> getUserList() {
        List<User> userList = this.list();
        return userList.stream().map(user -> {
            UserVO u = new UserVO();
            BeanUtil.copyProperties(user, u);
            return u;
        }).collect(Collectors.toList());
    }

    @Override
    public ResultData<String>  login(UserLoginReq userLoginReq) {
        // 1. 查询用户
        User user = this.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, userLoginReq.getUsername()));

        // 2. 用户不存在
        if (ObjectUtil.isEmpty(user)) {
            return ResultData.fail(ReturnCodeEnum.USERNAME_NOT_EXIST.getCode(),
                    ReturnCodeEnum.USERNAME_NOT_EXIST.getMessage());
        }

        // 3. 校验密码
        if (!DigestUtil.md5Hex(userLoginReq.getPassword()).equals(user.getPassword())) {
            return ResultData.fail(ReturnCodeEnum.USERNAME_OR_PASSWORD_ERROR.getCode(),
                    ReturnCodeEnum.USERNAME_OR_PASSWORD_ERROR.getMessage());
        }

        // 登录成功，记录用户id
        StpUtil.login(user.getId());
        // 返回token
        return ResultData.success(StpUtil.getTokenValue());
    }

    @Override
    public ResultData<String> logout() {
        // 判断用户是否登录
        if (!StpUtil.isLogin()) {
            return ResultData.fail(ReturnCodeEnum.NOT_LOGIN.getCode(),
                    ReturnCodeEnum.NOT_LOGIN.getMessage());
        }

        StpUtil.logout();
        return ResultData.success("注销登录成功");
    }

    @Override
    public ResultData<UserVO> getLoginUserInfo() {
        // 判断用户是否登录
        if (!StpUtil.isLogin()) {
            return ResultData.fail(ReturnCodeEnum.NOT_LOGIN.getCode(),
                    ReturnCodeEnum.NOT_LOGIN.getMessage());
        }

        // 获取用户信息
        UserVO userVo = new UserVO();
        BeanUtil.copyProperties(this.getById(StpUtil.getLoginIdAsLong()), userVo);
        return ResultData.success(userVo);
    }

    @Override
    public ResultData<UserVO> getUserById(Long id) {
        User user = this.getById(id);
        if (!ObjectUtil.isEmpty(user)) {
            UserVO userVo = new UserVO();
            BeanUtil.copyProperties(user, userVo);
            return ResultData.success(userVo);
        }
        return ResultData.fail(ReturnCodeEnum.USER_NOT_EXIST.getCode(), ReturnCodeEnum.USER_NOT_EXIST.getMessage());
    }

    @Override
    public UserVO getUserName(String username) {
        User one = this.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if(!ObjectUtil.isEmpty(one)){
            UserVO userVo = new UserVO();
            BeanUtil.copyProperties(one, userVo);
            return userVo;
        }
        return null;
    }

}




